<?php
/**
 * Implemented patterns: Lazy-load
 * @version 2.0
 **/
class WordPressContentHtmlReference {

    private $_cache = false;

    private $_contents = array();

    private $_loaded = false;

    private $_uri;

    public function __construct($uri) {
        $this->_cache = Configuration::get('Framework.WordPress.WordPressContentHtmlReference._cache');
        $this->_uri = $uri;
    }

    public function __get($name) {

        if (!$this->_loaded) {

            // Locals
            $Cache = null;
            $cached = null;

            // Try to get data from cache if cache is enabled
            if ($this->_cache) {
                $Cache = Cache::load();
                $cached = $Cache->get($this->_uri);
            }

            if (null != $cached) {

                Trace::writeLine("Framework.WordPress.WordPressContentHtmlReference.__get $this->_uri from cache");

                $this->_contents = $cached;
            } else {

                Trace::writeLine("Framework.WordPress.WordPressContentHtmlReference.__get $this->_uri from web");

                $this->parse();

                // Add data to cache if cache is enabled
                if (null != $Cache) {
                    $Cache->set($this->_uri, $this->_contents);
                    $Cache->save();
                }
            }

            $this->_loaded = true;
        }

        if ("content" == $name) {
            return $this->_contents;
        } else if ("url" == $name) {
            return $this->_uri;
        }

        return null;
    }

    public static function onFilterData($node) {

        $class = $node->attr['class'];
        if ('info' == $class ||
            'possibly-related' == $class ||
            'under' == $class ||
            'adcode' == $class ||
            'p-head' == $class ||
            'p-det' == $class ||
            'postmetadata' == $class ||
            'navigation' == $class ||
            'commentlist' == $class ||
            'post-content' == $class ||
            'commentlist snap_preview' == $class ||
            'pd-rating' == $class) {


            $node->outertext = '';
        }

        $id = $node->attr['id'];
        if ('comments' == $id ||
            'respond' == $id ||
            'wpl-likebox' == $id) {


            $node->outertext = '';
        }

        // v.1.0
        //if ("script" == $node->tag) {
        //    $node->outertext = '';
        //}
    }

    /**
     * Parses the given URI
     **/
    private function parse() {

        // v.1.0
        //$document = file_get_html($this->_uri);

        // v.2.0
        // Had to add custom JavaScript removal after WP changed something
        // in the blog engine
        $content = file_get_contents($this->_uri);
        $content = preg_replace("'<script[^>]*>.*</script>'siU", '', $content);
        $document = new simple_html_dom($content);
        $document->set_callback('WordPressContentHtmlReference::onFilterData');

        $i = 0;
        while (true) {

            // Extract WordPress post
            $post = $document->find('.post', $i);
            if (null == $post) {

                // Extract WordPress page
                $post = $document->find('.post-page', $i);
            }

            // Process extracted data
            if (null != $post) {
                $this->_contents[] = $post->innertext();
            } else {
                break;
            }

            $i++;
        }
    }
}
?>